package com.lvshi0797.cms.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.lvshi0797.cms.entities.FBaseInfo;
import com.lvshi0797.cms.entities.Pager;

@Repository("baseInfoDao")
public class BaseInfoDao extends BaseDao<FBaseInfo> implements IBaseInfoDao {

	@Override
	public List<FBaseInfo> listBaseInfoByMid(int mid) {

		String hql = "select new FBaseInfo"
				+ "(id,huzhuName,totalNum,disaNum,poorShuxing,poorCause,mainIncome,lastPerIncome,isOutPoor,outPoorDate,outPoorWay,town,vallige,team,contact) "
				+ "from FBaseInfo b where b.manager.id=?";
		return this.list(hql, mid);
	}

	@Override
	public Pager<FBaseInfo> findBaseInfoByMid(int mid) {
		String hql = "select new FBaseInfo"
				+ "(id,huzhuName,totalNum,disaNum,poorShuxing,poorCause,mainIncome,lastPerIncome,isOutPoor,outPoorDate,outPoorWay,town,vallige,team,contact) "
				+ "from FBaseInfo b where b.manager.id=?";
		return this.find(hql, mid);
	}

	@Override
	public void updateBaseInfo(FBaseInfo baseInfo) {
		String hql = "update FBaseInfo set "
				+ "contact=?, disaNum=?, huzhuName=?, "
				+ "isOutPoor=?, lastPerIncome=?, mainIncome=?, "
				+ "outPoorDate=?, outPoorWay=?, poorCause=?, "
				+ "poorShuxing=?, team=?, totalNum=?, "
				+ "town=?, vallige=? where id=?";
		Object[] args = new Object[] { baseInfo.getContact(), baseInfo.getDisaNum(),
				baseInfo.getHuzhuName(), baseInfo.getIsOutPoor(),
				baseInfo.getLastPerIncome(), baseInfo.getMainIncome(),
				baseInfo.getOutPoorDate(), baseInfo.getOutPoorWay(),
				baseInfo.getPoorCause(), baseInfo.getPoorShuxing(),
				baseInfo.getTeam(), baseInfo.getTotalNum(),
				baseInfo.getTown(), baseInfo.getVallige(),baseInfo.getId() };
		this.updateByHql(hql,args);
	}

	@Override
	public List<FBaseInfo> listBaseInfo(int mid, String huzhuName, String town,
			int isOutpoor, String startD, String endD) {

		StringBuffer hql = new StringBuffer("select new FBaseInfo"
				+ "(id,huzhuName,totalNum,disaNum,poorShuxing,poorCause,mainIncome,lastPerIncome,isOutPoor,outPoorDate,outPoorWay,town,vallige,team,contact) "
				+ "from FBaseInfo b where 1=1");
		if(mid>0){
			hql.append(" and b.manager.id="+mid);
		}
		if(huzhuName!=null){
			hql.append(" and huzhuName like '%"+huzhuName+"%'");
		}
		if(town!=null){
			hql.append(" and ( town like '%"+town+"%' or vallige like '%"+town+"%' or team like '%"+town+"%')");
		}
		if(isOutpoor!=-1){
			hql.append(" and isOutpoor ="+isOutpoor);
		}
		if(startD!=null){
			hql.append(" and addTime > '"+startD+"'");
		}
		if(endD!=null){
			hql.append(" and addTime < '"+endD+"'");
		}
		return this.list(hql.toString());
		
	}

	@Override
	public Pager<FBaseInfo> findBaseInfo(int mid, String huzhuName,
			String town, int isOutpoor, String startD, String endD) {
		getSession().clear();
		StringBuffer hql = new StringBuffer("select new FBaseInfo"
				+ "(id,huzhuName,totalNum,disaNum,poorShuxing,poorCause,mainIncome,lastPerIncome,isOutPoor,outPoorDate,outPoorWay,town,vallige,team,contact) "
				+ "from FBaseInfo b where 1=1");
		if(mid>0){
			hql.append(" and b.manager.id="+mid);
		}
		if(huzhuName!=null&&StringUtils.isNotEmpty(huzhuName)){
			hql.append(" and huzhuName like '%"+huzhuName+"%'");
		}
		if(town!=null&&StringUtils.isNotEmpty(town)){
			hql.append(" and ( town like '%"+town+"%' or vallige like '%"+town+"%' or team like '%"+town+"%')");
		}
		if(isOutpoor!=-1){
			hql.append(" and isOutpoor ="+isOutpoor);
		}
		if(startD!=null&&StringUtils.isNotEmpty(startD)){
			hql.append(" and addTime > '"+startD+"'");
		}
		if(endD!=null&&StringUtils.isNotEmpty(endD)){
			hql.append(" and addTime < '"+endD+"'");
		}
		return this.find(hql.toString());
	}

}
